package com.hazardSoft.eStore.servlets;

import java.io.IOException;
import java.sql.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hazardSoft.eStore.common.exception.SystemException;
import com.hazardSoft.eStore.entities.Offer;
import com.hazardSoft.eStore.entities.OfferType;
import com.hazardSoft.eStore.integration.OfferDelegate;

/**
 * Servlet implementation class OfferTest
 */
public class OfferTest extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OfferTest() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doAll(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doAll(request, response);
	}

	@SuppressWarnings("deprecation")
	private void doAll(HttpServletRequest request, HttpServletResponse response) {
		OfferDelegate offerDelegate;
		
		Offer offer;
		
		Date startdate;
		Date enddate;
		
		List<Offer> offerList;
		
		try 
		{
			offerDelegate = new OfferDelegate();
			
			offer = new Offer();
			offer.setConditions("C1,C2");
			offer.setDescription("D1,D2,D3");
			offer.setName("Of1");
			offer.setOffertype(new OfferType(1));
			offer.setPorcentage(25);
			offer.setQuantity(10);
			
			offer = offerDelegate.insertOffer(offer);
			
			System.out.println(offer.getId());
			
			System.out.println("///////////////////////////////////");
			System.out.println("///////////////////////////////////");
			System.out.println("///////////////////////////////////");
			
			
			offer = new Offer();
			offer.setConditions("C4,C5");
			offer.setDescription("D6,D8");
			offer.setName("Of2");
			offer.setOffertype(new OfferType(2));
			offer.setPorcentage(25);
			
			startdate = new Date(109,6,14);
			enddate = new Date(109,7,14);
			
			offer.setStartdate(startdate);
			offer.setEnddate(enddate);
			
			offer = offerDelegate.insertOffer(offer);
			
			System.out.println(offer.getId());
			
			System.out.println("///////////////////////////////////");
			System.out.println("///////////////////////////////////");
			System.out.println("///////////////////////////////////");
			
			offerList = offerDelegate.listOffers();
			
			for (Offer offerTemp : offerList) 
			{
				System.out.println(offerTemp.getName());
			}
			
			System.out.println("///////////////////////////////////");
			System.out.println("///////////////////////////////////");
			System.out.println("///////////////////////////////////");
			
			offer = new Offer();
			offer.setId(1);
			
			System.out.println(offerDelegate.removeOffer(offer));
			
			System.out.println(offerDelegate.searchOfferById(offer));
			
			System.out.println("///////////////////////////////////");
			System.out.println("///////////////////////////////////");
			System.out.println("///////////////////////////////////");
			
			offer = new Offer();
			offer.setId(2);
			
			offer = offerDelegate.searchOfferById(offer);
			
			offer.setName("Of2");
			offer.setConditions("C1");
			offer.setDescription("D1");
			offer.setPorcentage(5);
			offer.setQuantity(50);
			offer.setOffertype(new OfferType(1));
			offer.setEnddate(null);
			offer.setStartdate(null);
			
			System.out.println(offerDelegate.updateOffer(offer));
			
			offer = new Offer();
			offer.setName("Of2");
			
			offer = offerDelegate.searchOfferByName(offer);
			
			System.out.println(offer.getId());
			System.out.println(offer.getName());
			System.out.println(offer.getConditions());
			System.out.println(offer.getDescription());
			System.out.println(offer.getPorcentage());
			System.out.println(offer.getQuantity());
			System.out.println(offer.getOffertype());
			System.out.println(offer.getEnddate());
			System.out.println(offer.getStartdate());
			
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
